package com.soul.dj;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;
import android.widget.Toast;
import com.soul.dj.util.Util;

public class MainApplication extends Application {
	private static final String TAG = "MainApplication";
	public static Context applicationContext;
	private static MainApplication instance;

	private static List<Activity> activityList = new LinkedList<Activity>();
	private static long mExitTime;

	private static Map<String,Object> memeryMap = new HashMap<>();

	@Override
	public void onCreate() {
		super.onCreate();
		applicationContext = this;
		instance = this;
		
		String processName = Util.getProcessName(this,
				android.os.Process.myPid());
		Log.d(TAG, "application重建........" + processName);
	}

	public static MainApplication getInstance() {
		return instance;
	}

	// 添加Activity到容器中
	public static void addActivity(Activity activity) {
		activityList.add(activity);
	}

	// 删除Activity到容器中
	public static void removeActivity(Activity activity) {
		activityList.remove(activity);
		activity.finish();
	}

	// 遍历所有Activity并finish
	public static void exit() {
		for (Activity activity : activityList) {
			activity.finish();
		}
	}

	public static void exit(Context context) {

		if ((System.currentTimeMillis() - mExitTime) > 2000) {
			Toast.makeText(context, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			mExitTime = System.currentTimeMillis();
		} else {
			for (Activity activity : activityList) {
				activity.finish();
			}
			System.exit(0);
		}

	}

	public static void addMap(String key,Object value) {
		memeryMap.put(key,value);
	}

	public static Object getMap(String key){
		return memeryMap.get(key);
	}

	public static void removeMap(String key){
		memeryMap.remove(key);
	}
	public static void clearMap(){
		memeryMap.clear();
	}


	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		MultiDex.install(base);
	}
}
